package com.flute.framework.data.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import com.flute.framework.FluteMonitor;
import com.flute.framework.data.Sum;
import com.flute.framework.data.SumCheck;
import com.flute.framework.data.constants.Constants;
import com.flute.framework.monitor.EventLogger;
import com.flute.framework.util.EventLoggerUtil;
import com.flute.framework.util.FileToolsUtil;
import com.flute.tools.util.exceptoin.DataParserException;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer;
import com.thoughtworks.xstream.io.xml.XppDriver;

public class RawSumCheckParser {
	public static void main(String[] args) throws DataParserException {
		File file = new File(
				"E:\\home\\release\\lily\\rawData\\sumcheck\\sumcheck.xml");
		EventLogger.startNewEvent(FluteMonitor.getImporterKey());
		new RawSumCheckParser().parse(file);
	}

	public List<Sum> parse(File f) throws DataParserException {
		String fileName = f.getName();
		EventLoggerUtil.info("检查" + fileName + "是否有效XML文件", FluteMonitor
				.getImporterKey());
		if (!FileToolsUtil.isValidXMLFile(fileName)) {
			return null;
		}

		EventLoggerUtil.info("检查成功", FluteMonitor.getImporterKey());
		return RawSumCheckParser.parserSumCheckXml(f);
	}

	/**
	 * @param File
	 *            sum check xml
	 * @return SumCheck
	 * @throws Exception
	 */
	public static List<Sum> parserSumCheckXml(final File file)
			throws DataParserException {
		long begin = System.currentTimeMillis();
		XStream xstream = new XStream(new XppDriver(new XmlFriendlyReplacer(
				"_-", "_")));
		xstream.alias("sumCheck", SumCheck.class);
		xstream.aliasField("sums", SumCheck.class, "sums");
		xstream.alias("sum", Sum.class);
		xstream.aliasAttribute(Sum.class, "name", "name");
		xstream.aliasAttribute(Sum.class, "value", "value");

		FileInputStream in = null;
		InputStreamReader reader = null;
		SumCheck sumCheck = null;
		try {
			in = new FileInputStream(file);
			reader = new InputStreamReader(in, Constants.UTF8_CHARSET);
			sumCheck = (SumCheck) xstream.fromXML(reader);
			long end = System.currentTimeMillis();
			EventLoggerUtil.info("花费的时间：" + +(end - begin), FluteMonitor
					.getImporterKey());
		} catch (FileNotFoundException e) {
			throw new DataParserException(e);
		} catch (UnsupportedEncodingException e) {
			throw new DataParserException(e);
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					throw new DataParserException(e);
				} finally {
					reader = null;
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					throw new DataParserException(e);
				} finally {
					in = null;
				}
			}
		}
		return sumCheck.getSums();
	}
}
